//
//  MainTabBar.m
//  MyNewWeiBo
//
//  Created by wpapple on 16/5/30.
//  Copyright © 2016年 heron. All rights reserved.
//

#import "MainTabBar.h"

@interface MainTabBar()

@property (nonatomic,strong) UIButton * addBtn;

@end

@implementation MainTabBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UIButton *)addBtn{
    if (!_addBtn) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(compose:) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        _addBtn = btn;
        [self addSubview:_addBtn];
    }
    return _addBtn;
}

- (void)compose:(UIButton *)btn{
    
    if ([self.myDelegate respondsToSelector:@selector(mainTabBar:withButton:)]) {
        [self.myDelegate mainTabBar:self withButton:btn];
    }
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    int btnIndex = 0;
    CGFloat tabBarButtonW = self.width / 5.0;
    CGFloat tabBarButtonH = self.height;
    
    NSLog(@"%@",self.subviews);
    
    for (int i = 0; i < self.subviews.count; i++) {
        UIView * child = self.subviews[i];
        if ([child isKindOfClass:[UIControl class]]) {
            if (btnIndex == 2) {
                btnIndex++;
            }
            CGFloat tabBarButtonX = btnIndex * tabBarButtonW;
            child.frame = CGRectMake(tabBarButtonX, 0, tabBarButtonW, tabBarButtonH);
            btnIndex ++;
        }
    }
    
    self.addBtn.size = self.addBtn.currentBackgroundImage.size;
    self.addBtn.center = CGPointMake(self.width * 0.5, self.height * 0.5);
    
}

@end
